package com.gzx.plugin.tjzy.xxljob;

import cn.hutool.core.collection.CollectionUtil;
import com.gzx.plugin.tjzy.modular.sms.api.strategy.SMSStrategy;
import com.gzx.plugin.tjzy.modular.sms.api.strategy.SMSStrategyFactory;
import com.gzx.plugin.tjzy.modular.sms.batch.service.GzxSmsService;
import com.gzx.plugin.tjzy.modular.supplier.service.GzxSupplierService;
import com.gzx.plugin.tjzy.modular.supplier.vo.GzxSupplierVo;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import vip.xiaonuo.common.tenant.TenantContext;
import vip.xiaonuo.common.util.CommonDateUtil;

import java.util.List;

/**
 * @author zjt
 * @description
 * @date 2025/3/11 16:46
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SMSXxlJob {

    private final GzxSmsService smsService;

    private final GzxSupplierService supplierService;

    private final SMSStrategyFactory smsStrategyFactory;

    //短信余额告警
    @XxlJob("syncSMSBalanceJobHandler")
    public void syncSMSBalanceJobHandler() {
        log.info("XxlJob定时任务启动--------------->短信余额告警{}", CommonDateUtil.getCurrentTimeStr());
    }

    @XxlJob("autoSendSMSJobHandler")
//    @CommonCheckInstanceOnline
    public void autoSendSMSJobHandler() {
        log.info("XxlJob定时任务启动--------------->自动发送短信{}", CommonDateUtil.getCurrentTimeStr());
        TenantContext.ignoreHandle(smsService::autoSend);
    }

    @XxlJob("syncSMSReceiptJobHandler")
//    @CommonCheckInstanceOnline
    public void syncSMSReceiptJobHandler() {
        log.info("XxlJob定时任务启动--------------->主动获取短信发送回执{}", CommonDateUtil.getCurrentTimeStr());
        List<GzxSupplierVo> supplierVoList = TenantContext.ignoreHandle(supplierService::list);
        if (CollectionUtil.isNotEmpty(supplierVoList)) {
            for (GzxSupplierVo supplierVo : supplierVoList) {
                TenantContext.ignoreHandle(() -> {
                    log.info("主动获取短信发送状态：{}，租户校验:{}", supplierVo.getSupplierCode(), TenantContext.isFilterEnabled());
                    SMSStrategy smsStrategy = smsStrategyFactory.getStrategy(supplierVo.getSupplierCode());
                    smsStrategy.syncReceiptProcess();
                });
            }
        }
    }

    @XxlJob("syncSMSReplyProcessJobHandler")
//    @CommonCheckInstanceOnline
    public void syncReplyProcessJobHandler() {
        log.info("XxlJob定时任务启动--------------->主动获取短信回复{}", CommonDateUtil.getCurrentTimeStr());
        List<GzxSupplierVo> supplierVoList = TenantContext.ignoreHandle(supplierService::list);
        if (CollectionUtil.isNotEmpty(supplierVoList)) {
            for (GzxSupplierVo supplierVo : supplierVoList) {
                Thread.startVirtualThread(() -> TenantContext.ignoreHandle(() -> {
                    SMSStrategy smsStrategy = smsStrategyFactory.getStrategy(supplierVo.getSupplierCode());
                    smsStrategy.syncReplyProcess();
                }));
            }
        }
    }

    @XxlJob("smsWarnJobHandler")
//    @CommonCheckInstanceOnline
    public void smsWarnJobHandler() {
        try {
            TenantContext.disableTenantFilter();
            smsService.smsWarn();
        } catch (Exception e) {
            log.error("短信告警出现异常", e);
        } finally {
            TenantContext.enableTenantFilter();
            TenantContext.clear();
        }
    }
}
